package ch05
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.media.Camera;
	import flash.media.Video;

	public class CameraBitmap extends Sprite
	{
		private var _cam:Camera;
		private var _vid:Video;
		private var _bmpd:BitmapData;

		public function CameraBitmap()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;

			_cam = Camera.getCamera();
			_cam.setMode(320, 240, 15);
			_vid = new Video(320, 240);
			_vid.attachCamera(_cam);

			_bmpd = new BitmapData(320, 240, false);
			addChild(new Bitmap(_bmpd));

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			/**
			 * 反转图像
			 * 要知道，用户在摄像头前所做的反应，总希望和镜子中一样，人往左，视频中人也往
			   左，人往右，其也往右。但事实并不是这样，所以需要我们来为之做反转图像。
			 */
			_bmpd.draw(_vid, new Matrix(-1, 0, 0, 1, _bmpd.width, 0));
		}
	}
}